package cn.sun.user.service.impl;

import cn.sun.basic.service.impl.BaseServiceImpl;
import cn.sun.basic.util.MD5Utils;
import cn.sun.basic.util.StrUtils;
import cn.sun.user.domain.LoginInfo;
import cn.sun.user.domain.User;
import cn.sun.user.dto.UserDto;
import cn.sun.user.mapper.LoginInfoMapper;
import cn.sun.user.mapper.UserMapper;
import cn.sun.user.query.UserQuery;
import cn.sun.user.service.IUserService;
import cn.sun.user.util.UserConstants;
import com.sun.org.apache.bcel.internal.classfile.Constant;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.rmi.registry.Registry;
import java.util.List;

@Service
public class UserServiceImpl extends BaseServiceImpl<User,UserQuery> implements IUserService {
    @Autowired
    private UserMapper userMapper;

    @Autowired
    private LoginInfoMapper loginInfoMapper;

    @Autowired
    @Qualifier("redisTemplate")
    private RedisTemplate redisTemplate;

    @Transactional
    @Override
    public void save(User user) {
        LoginInfo loginInfo =new LoginInfo();
        BeanUtils.copyProperties(user, loginInfo);
        loginInfoMapper.save(loginInfo);
        user.setLogininfo(loginInfo);
        userMapper.save(user);
    }

    @Transactional
    @Override
    public void update(User user) {
        super.update(user);
    }

    @Transactional
    @Override
    public void delete(Long id) {
        super.delete(id);
    }

    @Override
    public void batchdel(List<Long> ids) {
        userMapper.batchdel(ids);
    }

    @Transactional
    @Override
    public void phoneRegister(UserDto userDto) {
        String phone = userDto.getPhone();
        String verifyCode = userDto.getVerifyCode();
        String password = userDto.getPassword();
        String confirmPwd = userDto.getConfirmPwd();
        System.out.println(phone+verifyCode+password+confirmPwd);
        //1 基本校验
        //1.1 空检验
        if (phone==null ||password==null ||verifyCode==null||confirmPwd==null){
            throw new RuntimeException("请输入完整的信息");
        }
        //1.2 两个密码要一致
        if (!password.equals(confirmPwd)){
            throw new RuntimeException("两次密码不一致，请确认");
        }
        //1.3 判断是否已经被注册
        User userTemp = userMapper.findByPhone(phone);
        if (userTemp!=null){
            throw new RuntimeException("该用户已被注册！");
        }

        //2 验证验证码-吧输入验证码和redis中存的验证码做比对（不要区分大小写）
        Object obj = redisTemplate.opsForValue().get(UserConstants.REGISTER_CODE_PREFIX + phone);
        String savedCode=String.valueOf(obj).split(":")[0];

        if (!verifyCode.equalsIgnoreCase(savedCode)) {
            throw new RuntimeException("验证码错误！");
        }
        //3 注册
        //将注册表的信息装换成用户信息
        User user = registerInfo2User(userDto);
        LoginInfo info = User2LoginInfo(user);
        //3.2 登录信息表
        loginInfoMapper.save(info);
        user.setLogininfo(info);
        //3.1 用户表
        userMapper.save(user);
    }

    private LoginInfo User2LoginInfo(User user) {
        LoginInfo loginInfo=new LoginInfo();
        BeanUtils.copyProperties(user,loginInfo); //按照同名原则拷贝属性
        return loginInfo;
    }

    private User registerInfo2User(UserDto userDto) {
        //1 申明要返回对象
        User user = new User();
        //2 进行转换
        user.setUsername(userDto.getPhone());
        user.setPhone(userDto.getPhone());
        String salt = StrUtils.getComplexRandomString(32);
        String securityPwd = MD5Utils.encrypByMd5(salt+userDto.getPassword());
        user.setSalt(salt);
        user.setPassword(securityPwd);
        //3 返回
        return user;
    }
}
